package com.green.business.controller;

import com.green.business.domain.Goods;
import com.green.business.domain.Inport;
import com.green.business.service.GoodsService;
import com.green.business.service.InportService;
import com.green.business.vo.InportVo;
import com.green.system.common.ActiveUser;
import com.green.system.common.Constant;
import com.green.system.common.ResultObj;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//import sun.plugin.liveconnect.SecureInvocation;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;



/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Green
 * @Date: 2021/10/22/16:31
 * @Description:
 */

@RequestMapping("inport")
@RestController
public class InportController {

    @Autowired
    private InportService inportService ;

    @Autowired
    private GoodsService goodsService;

    /**
     * 查询
     */
    @RequestMapping("loadAllInport")
    public Object loadAllInport(InportVo inportVo){
        return this.inportService.queryAllInport(inportVo);
    }

    /**
     * 添加
     */
    @RequestMapping("addInport")
    public ResultObj addInport(InportVo inportVo){
        try{
            ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();

            //从前端传回的数据进行实体封装 Inport
            Inport inport = new Inport();
            inport.setProviderid(inportVo.getProviderid());
            inport.setPaytype(inportVo.getPaytype());
            inport.setNumber(inportVo.getNumber());
            inport.setRemark(inportVo.getRemark());
            inport.setInportprice(inportVo.getInportprice());
            inport.setGoodsid(this.goodsService.selectMaxGoodsId());
            inport.setInporttime(new Date());
            inport.setOperateperson(activeUser.getUser().getName());

            //Goods
            Goods goods = new Goods();
            goods.setId(inport.getId());
            goods.setGoodsname(inportVo.getGoodsname());
            goods.setProduceplace(inportVo.getProduceplace());
            goods.setSize(inportVo.getSize());
            goods.setGoodspackage(inportVo.getGoodspackage());
            goods.setProductcode(inportVo.getProductcode());
            goods.setPromitcode(inportVo.getPromitcode());
            goods.setDescription(inportVo.getDescription());
            goods.setProviderid(inportVo.getProviderid());
            goods.setNumber(inportVo.getNumber());
            goods.setDangernum(inportVo.getDangernum());
            goods.setGoodsimg(inportVo.getGoodsimg());
            goods.setProductiondate(inportVo.getProductiondate());
            goods.setExpiration(inportVo.getExpiration());
            //进货添加商品时将商品可用置0，需要在商品管理处上架
            goods.setAvailable(Constant.AVAILABLE_FALSE);

            this.inportService.saveInport(inport);
            this.goodsService.saveGoods(goods);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }

    }

    /**
     * 更新
     */
    @RequestMapping("updateInport")
    public ResultObj updateInport(Inport inport){
        try{
            this.inportService.updateInport(inport);  //update() updateSelective()
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }

    }

    /**
     * 删除
     */
    @RequestMapping("deleteInport")
    public ResultObj deleteInport(Integer id){
        try {
            this.inportService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     */
    @RequestMapping("batchDeleteInport")
    public ResultObj batchDeleteInport(Integer[] ids){
        try {
            if (null != ids && ids.length > 0){
                List<Integer> idsList = new ArrayList<>();
                for (Integer id : ids) {
                    idsList.add(id);
                }
                this.inportService.removeByIds(idsList);
                return ResultObj.DELETE_SUCCESS;
            }else{
                return new ResultObj(-1,"传入ID不能为空");
            }

        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}
